/*
 *Description:
 *User:laoHu
 *Created with Intellij IDEA 2025
 *Data:2025.06.06
 *Time:09:10
*/

import Demo1.Shape;
import Demo1.cycle;
import Demo1.flower;
import Demo1.rect;
import Demo2.Animal;
import Demo2.Dog;
import Demo2.Duck;
import Demo2.Frog;
import Demo3.Computer;
import Demo3.IMouse;
import Demo3.KeyBoard;

import javax.management.openmbean.ArrayType;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;
import java.lang.Comparable;
class Person{
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
//系统的：
//    @Override
//    public boolean equals(Object o) {
//        if (o == null || getClass() != o.getClass()) return false;
//        Person person = (Person) o;
//        return age == person.age && Objects.equals(name, person.name);
//    }
    //模拟实现
    @Override
    public boolean equals(Object obj){
        if(obj == null) return false;
        if(this == obj) return true;
        if(!(obj instanceof Person))
            return false;
        //向下转型
       Person person = (Person) obj;
        return (this.name.equals(person.name) && this.age == person.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

class Student implements Comparable {

    String name;
    int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Object o) {

        Student o1 = (Student) o;

        if (this.age > o1.age) return 1;
        else if (this.age == o1.age) return 0;
        else return -1;
    }
}

class Stu {
    String name;
    int age;

    public Stu(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
//使用比较器
class scoreCompare implements Comparator<Stu>{

  @Override
    public int compare(Stu o1,Stu o2){
      return o1.age - o2.age;
  }
}
class nameCompare implements Comparator<Stu>{

    @Override
    public int compare(Stu o1,Stu o2){
        return o1.name.compareTo(o2.name);
    }
}


public class Main{

    //comparator比较器
    public static void main(String[] args) {

        Stu stu2  = new Stu("ss",22);
        Stu stu1 = new Stu("xx",11);

        nameCompare nameCompare = new nameCompare();
        scoreCompare scoreCompare = new scoreCompare();

        System.out.println(nameCompare.compare(stu1,stu2));
        System.out.println(scoreCompare.compare(stu1,stu2));
    }

    //使用comparable接口
    public static void main6(String[] args) {
        Student studetn1 = new Student("lisi",20);
        Student student2 = new Student("wansi",59);

        System.out.println(studetn1.compareTo(student2));;
    }

    //Object类:所有类发父类
    public static void main5(String[] args) {
        //如何类型都能接收
//      Object o = 10;
//      Object str = "ssss";
//      Object s  = 10.00;
//      Object a = new int[4];

        //equal方法
        Person person1 = new Person("123",3);
        Person person2 = new Person("123",3);

        //法一：
      //直接比较:引用不相等输出false
       // System.out.println(person1 == person2);
        //使用Object的equal:应为引用不同输出也不同
        //默认的equal方法
//        public boolean equal(Object obj){
//            return (this == obj);
//        }
       // System.out.println(person1.equals(person2));

        //法二：重写equal方法
        //直接比较:引用不相等输出false
         System.out.println(person1 == person2);
       // 使用重写Object的equal:应为引用不同输出也不同
         System.out.println(person1.equals(person2));

         //hasCode方法
        //默认@IntrinsicCandidate
        //    public native int hashCode();
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());
    }

    //接口的”继承“,使用extends关键字
    public static void main4(String[] args) {
        //实例化对象
        Dog dog =new Dog("踩踩",4);
        Duck duck = new Duck("瓦瓦",3);
        Frog frog = new Frog("嘻嘻",2);
        //非动物
        Robot robot =new Robot("人机");

        //使用
        dog.run();
        duck.swim();
        frog.eat();
        frog.run();
        //不用关注对象的类型是什么，主要有这种特性就可以使用
        robot.run();

    }

    //面试题：接口和出抽象类的区别
    //抽象类中包含抽象方法和普通方法和成员变量，接口可以包含成员变量和抽象方法
    //都是其成员变量是被public static final 修饰的，抽象方法是被public abstract
    //修饰的，并且接口中不能包含普通方法。

    //接口的使用
    public static void main3(String[] args) {

        Computer computer = new Computer();
        //7。接口不能实例化
        //Usb usb = new Usb();
        //打开
        computer.powerOn();
        //使用
        computer.useComputer(new IMouse());

        computer.useComputer(new KeyBoard());

        //关闭
        computer.powerOff();
    }

    public static void func(Animal animal){
        animal.eat();
    }

    //抽象类
    public static void main2(String[] args) {

        //实例对象
        Dog dog =  new Dog("玩玩",2);
        Duck duck = new Duck("哒哒",5);
        Frog frog = new Frog("哇哇",6);
        //调用方法
        func(dog);
        func(duck);
        func(frog);
    }

    public static void fun(Shape shape){
        shape.draw();
    }

public static void main1(String[] args) {

    rect rect = new rect();
    flower flower =new flower();
    cycle cycle = new cycle();

    //多态
    fun(rect);
    fun(flower);
    fun(cycle);
  }
}
